const express = require("express");
const route = express.Router();
//引入权限校验中间件
const isLoginMiddle = require("../middleware/isLoginMiddle")

const advModel = require("../db/model/addAdvModel")

route.get("/advList/:pageNo/:everyNum", isLoginMiddle, async (req, res) => {
  //查询所有的广告列表集合的数据 
  const { pageNo, everyNum } = req.params //params参数 
  //pageNo 当前页面 everyNum一页几个
  try {
    //limit()限制长度个数一页有几个 skip()跳过几个
    const findResult = await advModel.find({}).limit(everyNum).skip((pageNo - 1) * 3);
    //count() 请求的总数量
    const allAdvResult = await advModel.count({})
    res.send({
      code: 10000,
      maesage: "查询成功",
      data: {
        advLists: findResult,
        advCount: Math.ceil(allAdvResult / everyNum)  //查询总数量除以每页几个 向上取整
      }
    })
  } catch (e) {
    console.log("查询广告列表异常" + e.message);
    res.send({
      code: 10001,
      mesage: "查询广告列表异常" + e.message,
      data: null
    })
  }

})

module.exports = route;